// pages/Invite-code/Invite-code.js
const apiurl = require('../../config').apiurl;
import {
  Pulibc
} from '../../models/public.js'
const pulibc = new Pulibc();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    indicatorDots: !1,
    autoplay: !0,
    interval: 5e3,
    duration: 1e3,
    previousmargin: "137rpx",
    nextmargin: "137rpx",
    circular: !0,
    tuhight: 0,
    backImg: [],
    tuhight: 0,
    sharCdoe: '',
    saveImageBool: '', //保存图片授权
    nvitation_text: [],
    commdate:{} // 邀请码背景图
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getImg();
    var sharCdoe = wx.getStorageSync('member_id')
    this.setData({
      sharCdoe
    })
  },
  // 获取图片
  getImg() {
    let key = wx.getStorageSync('key');
    let url = apiurl + '/member/invite'
    wx.request({
      url: url,
      data: {
        key
      },
      success: data => {
        if (data.data.code == 200) {
          var commdate = data.data.datas.commdate
          var backImg = data.data.datas.img
          var nvitation_text = data.data.datas.nvitation_text
          console.log(data);
          
          this.setData({
            backImg,
            nvitation_text,
            commdate
          })
        }else if(data.data.code == 400){
          pulibc.navigateUrl('../register/register');
        }

      }
    })
  },
  // 切换生成二维码
  bindchange: function(e) {
    console.log(e)
    var backImg = this.data.backImg;
    this.setData({
      tuhight: e.detail.current
    })
  },

  // 海报分享
  sharePoster() {
    let that = this;
    var key = wx.getStorageSync('key');
    var imgid = this.data.tuhight;
    var url = apiurl + '/member/doPageShareposter';
    wx.showLoading({
      title: "图片保存中"
    }), wx.request({
      url: url,
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        key,
        imgid
      },
      success: data => {
        wx.downloadFile({
          url: data.data.datas,
          success: data => {
            console.log(data)
            wx.saveImageToPhotosAlbum({
              filePath: data.tempFilePath,
              success: res => {
                console.log(res)
                that.setData({
                  saveImageBool: true
                })
                wx.showToast({
                  title: "保存成功",
                  icon: "success",
                  duration: 2e3
                })
              },
              fail: err => {
                console.log("saveimg", err)
                that.setData({
                  saveImageBool: false
                })
                // 隐藏loading
                wx.hideLoading();
                // fail信息为未授权时，才调用。
                if (err.errMsg.indexOf('auth') != -1) {
                  that.getPromission();
                }
              }
            })
          }
        })


      }
    })
  },
  // 用户授权保存到相册
  getPromission() {
    let that = this;
    console.log("saveImageBool", that.data.saveImageBool)
    if (that.data.saveImageBool == false) {
      wx.showModal({
        title: '用户未授权',
        content: '请开启相应权限哦',
        showCancel: false,
        success: res => {
          if (res.confirm) {
            wx.openSetting({
              success: data => {
                console.log("data", data)
                //   console.log(Object.values(data.authSetting))
                // let authSettingArr=Object.values(data.authSetting);
                //   if (authSettingArr.indexOf(false) !== -1){
                //     that.getPreserve();
                //   }else {
                //     that.getPreserve();
                //   }
                if (data.authSetting["scope.writePhotosAlbum"] == true) {
                  that.sharePoster();
                } else {
                  that.getPromission();
                }
              }
            })
          }
        }
      })
    }
  },
  getPoster(data) {

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (wx.getStorageSync("key") == false) {
      wx.navigateTo({
        url: "../register/register"
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    var uid = wx.getStorageSync('member_id')
    var commdate = this.data.commdate;
    console.log(uid)

    return {
      path: '/pages/register/register?uid=' + uid,
      imageUrl: commdate.invite_forward
    }
  }
})